package com.b2b.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.b2b.exception.CustomException;
import com.b2b.model.CompanyInfoDto;
import com.b2b.model.WalletDto;
import com.b2b.model.entity.User;
import com.b2b.model.entity.company.CompanyInfo;
import com.b2b.service.CompanyInfoService;
import com.b2b.service.WalletService;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class CompanyInfoUtil {
    private final CompanyInfoService companyInfoService;
    private final WalletService walletService;

    public CompanyInfoUtil(CompanyInfoService companyInfoService,
                           WalletService walletService) {
        this.companyInfoService = companyInfoService;
        this.walletService = walletService;
    }

    public String getCompanyName(Integer companyId){
        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo.setCompanyId(companyId);
        companyInfo = companyInfoService.selectByPrimaryKey(companyInfo);
        String companyName = companyInfo.getCompanyName();
        return companyName;
    }

    public Integer insertCompanyInfo(CompanyInfoDto companyInfoDto){
        CompanyInfo companyInfo = getCompanyInfo(companyInfoDto);

        char state = '0';
        companyInfo.setState(state);

        Integer count = companyInfoService.insert(companyInfo);
        if (count<=0){
            return count;
        }else {
            count = companyInfo.getCompanyId();
            return count;
        }
    }

    public Integer updateInfo(CompanyInfoDto companyInfoDto){
        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo = getCompanyInfo(companyInfoDto);
        if (companyInfo.getImgUrlList().equals("null")){
            companyInfo.setImgUrlList(null);
        }
        companyInfo.setCompanyName(null);
        companyInfo.setBusinessLicense(null);
        companyInfo.setCorporationName(null);
        companyInfo.setRegisteredArea(null);

        return companyInfoService.updateByPrimaryKeySelective(companyInfo);
    }

    public Integer getCompanyIdByName(String companyName){
        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo.setCompanyName(companyName);
        Integer count = companyInfoService.selectCount(companyInfo);
        if (count <=0 ){
            throw new CustomException("公司名称错误！！");
        }
        companyInfo = companyInfoService.selectOne(companyInfo);
        Integer companyId = companyInfo.getCompanyId();

        return companyId;
    }
    public CompanyInfoDto getById(Integer companyId){
        CompanyInfo companyInfo = companyInfoService.selectByPrimaryKey(companyId);

        return getCompanyInfoDto(companyInfo);
    }
    public Integer insertCompany(User user){
            CompanyInfo companyInfo = new CompanyInfo();
            companyInfo.setMasterPhone(user.getPhone());
            Integer count = companyInfoService.insert(companyInfo);
            if (count >= 0){
                Integer companyId = companyInfo.getCompanyId();
                WalletDto walletDto = new WalletDto();
                walletDto.setCompanyId(companyId);
                count = walletService.insert(walletDto);
                if (count > 0){
                    return companyId;
                }else {
                    return count;
                }
            }else {
                return count;
            }
    }

    public CompanyInfoDto getCompanyInfoDto(CompanyInfo companyInfo){
        CompanyInfoDto companyInfoDto = new CompanyInfoDto();

        companyInfoDto.setCompanyId(companyInfo.getCompanyId());
        companyInfoDto.setCompanyName(companyInfo.getCompanyName());
        companyInfoDto.setCompanyType(companyInfo.getCompanyType());
        companyInfoDto.setBrand(companyInfo.getBrand());
        companyInfoDto.setAnnualTurnover(companyInfo.getAnnualTurnover());
        companyInfoDto.setBusinessLicense(companyInfo.getBusinessLicense());
        companyInfoDto.setCorporationName(companyInfo.getCorporationName());
        companyInfoDto.setEmployees(companyInfo.getEmployees());
        companyInfoDto.setEmail(companyInfo.getEmail());
        companyInfoDto.setMainBusiness(companyInfo.getMainBusiness());
        companyInfoDto.setMainProductsServices(companyInfo.getMainProductsServices());
        companyInfoDto.setManagementCertification(companyInfo.getManagementCertification());
        companyInfoDto.setQualityCertification(companyInfo.getQualityCertification());
        companyInfoDto.setMasterPhone(companyInfo.getMasterPhone());
        companyInfoDto.setMonthlyProduction(companyInfo.getMonthlyProduction());
        companyInfoDto.setRegisteredArea(companyInfo.getRegisteredArea());
        companyInfoDto.setRegisteredTime(companyInfo.getRegisteredTime());
        companyInfoDto.setRegisteredCapital(companyInfo.getRegisteredCapital());
        companyInfoDto.setPlantArea(companyInfo.getPlantArea());
        companyInfoDto.setMainSalesArea(companyInfo.getMainSalesArea());

        List<String> imgUrlList = JSONObject.parseArray((companyInfo.getImgUrlList()), String.class);

        companyInfoDto.setImgUrlList(imgUrlList);

        companyInfoDto.setState(String.valueOf(companyInfo.getState()));

        return companyInfoDto;
    }
    public CompanyInfo getCompanyInfo(CompanyInfoDto companyInfoDto){
        CompanyInfo companyInfo = new CompanyInfo();

        companyInfo.setCompanyId(companyInfoDto.getCompanyId());
        companyInfo.setCompanyName(companyInfoDto.getCompanyName());
        companyInfo.setCompanyType(companyInfoDto.getCompanyType());
        companyInfo.setBrand(companyInfoDto.getBrand());
        companyInfo.setAnnualTurnover(companyInfoDto.getAnnualTurnover());
        companyInfo.setBusinessLicense(companyInfoDto.getBusinessLicense());
        companyInfo.setCorporationName(companyInfoDto.getCorporationName());
        companyInfo.setEmployees(companyInfoDto.getEmployees());
        companyInfo.setEmail(companyInfoDto.getEmail());
        companyInfo.setMainBusiness(companyInfoDto.getMainBusiness());
        companyInfo.setMainProductsServices(companyInfoDto.getMainProductsServices());
        companyInfo.setManagementCertification(companyInfoDto.getManagementCertification());
        companyInfo.setQualityCertification(companyInfoDto.getQualityCertification());
        companyInfo.setMasterPhone(companyInfoDto.getMasterPhone());
        companyInfo.setMonthlyProduction(companyInfoDto.getMonthlyProduction());
        companyInfo.setRegisteredArea(companyInfoDto.getRegisteredArea());
        companyInfo.setRegisteredTime(companyInfoDto.getRegisteredTime());
        companyInfo.setRegisteredCapital(companyInfoDto.getRegisteredCapital());
        companyInfo.setPlantArea(companyInfoDto.getPlantArea());
        companyInfo.setMainSalesArea(companyInfoDto.getMainSalesArea());
        String imgUrlList = JSON.toJSONString(companyInfoDto.getImgUrlList());
        companyInfo.setImgUrlList(imgUrlList);
//        companyInfo.setState(companyInfoDto.getState());

        return companyInfo;
    }
}
